<?php  



function execCurlRequest($url, $postFields, $request){	

	$curl = curl_init($url);
	if(empty($curl)){
		die("ERREUR curl_init : Il semble que cURL ne soit pas disponible.");
	}

	curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-type: application/json'));
	curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);     	 	
	curl_setopt($curl,CURLOPT_HEADER, false);      			
  curl_setopt($curl,CURLOPT_FAILONERROR, false);
	curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request);
	if($request != "DELETE" && $request != "GET"){
		curl_setopt($curl,CURLOPT_POSTFIELDS, json_encode($postFields));	
	}
	//curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
	
	$content=curl_exec($curl);
  if(curl_errno($curl)){
		$error['erreurType'] = 'CURL';
    $error['code'] = curl_errno($curl);
		$error['message'] = curl_error($curl);
		return $error;
  }
	
	curl_close($curl);
	return $content;
}



// http://orioue.appspot.com/rest/collabs/



///
///	HTTP GET
///
function getSkillList(){
	$url = "http://orioue.appspot.com/rest/skills";
	$res = execCurlRequest($url, null, "GET");
	return json_decode($res, true);
}

function getSkillId($id){
	$url = "http://orioue.appspot.com/rest/skills/".$id;
	$res = execCurlRequest($url, null, "GET");
	return json_decode($res, true);
}

function getSkillSub($id){
	$url = "http://orioue.appspot.com/rest/skills/".$id."/subs";
	$res = execCurlRequest($url, null, "GET");
	return json_decode($res,true);
	
}
 
function getCollabs(){
	$url = "http://orioue.appspot.com/rest/collabs";
	$res = execCurlRequest($url, null, "GET");
	return json_decode($res, true);
}

function getCollabId($id){
	$url = "http://orioue.appspot.com/rest/collabs/".$id;
	$res = execCurlRequest($url, null, "GET");
	return json_decode($res,true);
}

function getCollabIdProfile($id) {
	$url = "http://orioue.appspot.com/rest/collabs.".$id."/profile";
	$res = execCurlRequest($url, null, "GET");
	return json_decode($res,true);
}

function getCollabIdSkills($id) {
	$url = "http://orioue.appspot.com/rest/skills/".$id."/skills";
	$res = execCurlRequest($url, null, "GET");
	return json_decode($res,true);
}

// ex: skillsId = "2+3+4"	
function getCollabsQueryAll($skillsId){
	$url = "http://orioue.appspot.com/rest/skills/query/all/skills=".$skillsId;
	$res = execCurlRequest($url, null, "GET");
	return json_decode($res,true);
}	

function getCollabsQueryOneOf($skillsId){
	$url = "http://orioue.appspot.com/rest/skills/query/oneOf/skills=".$skillsId;
	$res = execCurlRequest($url, null, "GET");
	return json_decode($res,true);
}

///
///	HTTP POST
///

// Postfield collabs
function postFieldsAddLastName(&$postFields, $lastName){
	$postFields['profile']['name'] = $lastName;
}

function postFieldsAddFirstName(&$postFields, $firstName){
	$postFields['profile']['firstname'] = $firstName;	
}

function postFieldsAddAddress(&$postFields, $address){
	$postFields['profile']['address'] = $address;
}

function postFieldsAddPhone(&$postFields, $phone){
	$postFields['profile']['phone'] = $phone;
}

function postFieldsAddEmail(&$postFields, $email){
	$postFields['profile']['email'] = $email;
}

function postFieldsAddFunction(&$postFields, $function){
	$postFields['profile']['function'] = $function;
}

function postFieldsAddSkill(&$postFields, $skills){
	$postFields['skills'] =  $skills;
}

function addCollab($postFields){
	$url = "http://orioue.appspot.com/rest/collabs";
	$res = execCurlRequest($url, $postFields, "POST");
  return $res;

}


// PostFields skills
function postFieldsAddName(&$postFields, $name){
	$postFields['name'] =  $name;
}

function postFieldsAddDescription(&$postFields, $description){
	$postFields['description'] =  $description;
}

function postFieldsAddParentID(&$postFields, $parentID){
	$postFields['parent_id'] =  $parentID;
}

function addSkill($postFields){
	$url = "http://orioue.appspot.com/rest/skills";
	$res = execCurlRequest($url, $postFields, "POST");
	return $res;
}

function addSkillToCollab($idSkill, $idCollab){
	$url = "http://orioue.appspot.com/rest/collabs".$idCollab."/skills/".$idSkill;
	$res = execCurlRequest($url, "", "POST");

}


///
///	HTTP DELETE
///

function deleteCollabs(){
	$url = "http://orioue.appspot.com/rest/collabs";
	return execCurlRequest($url, null, "DELETE");
}

function deleteCollab($id){

	$url = "http://orioue.appspot.com/rest/collabs/".$id;
	return execCurlRequest($url, null, "DELETE");
}

function deleteSkills(){
	$url = "http://orioue.appspot.com/rest/skills";
	return execCurlRequest($url, null, "DELETE");
}

function deleteSkill($idSkill){
	$url = "http://orioue.appspot.com/rest/skills/".$idSkill;
	return execCurlRequest($url, null, "DELETE");
}

function deleteSkillFromCollab($idCollab, $idSkill){
	$url = "http://orioue.appspot.com/rest/collabs/".$idCollab."/skills/".$idSkill;
	return execCurlRequest($url, null, "DELETE");
}


///
///	HTTP PUT
///


function putSkillId(&$postFields, $idSkill){     
	$url = "http://orioue.appspot.com/rest/skills/".$idSkill;
	return execCurlRequest($url, $postFields, "PUT");
	
}

function putCollabIdProfile(&$postFields, $id){
	$url = "http://orioue.appspot.com/rest/collabs/".$id."/profile";
	return execCurlRequest($url, $postFields, "PUT");
}

function postCollabIdSkill($id1, $id2){
	$url = "http://orioue.appspot.com/rest/collabs/".$id1."/skills/".$id2;
	$res = execCurlRequest($url, null, "POST");

}

?>
